﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FlowChartDemo
{
    public partial class FormDemo02V1 : FormBase
    {
        public FormDemo02V1()
        {
            InitializeComponent();
            DemoTitle = "第03节随课Demo  Part1";
            DemoNote = "效果：在两个矩形中添加【一条】直线，连接两个形状的中心";
        }

        /// <summary>
        /// 矩形定义
        /// </summary>
        public class RectShape
        {
            /// <summary>
            /// 矩形ID
            /// </summary>
            public string Id { get; set; }
            /// <summary>
            /// 矩形位置和尺寸
            /// </summary>
            public Rectangle Rect { get; set; }
        }

        /// <summary>
        /// 当前界面矩形集合
        /// </summary>
        List<RectShape> Shapes = new List<RectShape>();

        /// <summary>
        /// 画一个矩形(不同颜色)
        /// </summary>
        /// <param name="g"></param>
        /// <param name="shape"></param>
        void DrawShape2(Graphics g, RectShape shape)
        {
            var index = Shapes.FindIndex(a => a.Id == shape.Id);
            g.FillRectangle(GetBrush(index), shape.Rect);
            g.DrawString(shape.Id, Font, Brushes.White, shape.Rect);
        }

        /// <summary>
        /// 重新绘制当前所有矩形和连线
        /// </summary>
        /// <param name="g"></param>
        void DrawAll(Graphics g)
        {
            g.Clear(panel1.BackColor) ;
            foreach (var sp in Shapes)
            {
                DrawShape2(g, sp);
            }

            if (!LinePointStart.IsEmpty&& !LinePointEnd.IsEmpty)
            {
                //如果连线两个端点不是空的，则绘制连线
                g.DrawLine(Pens.Black, LinePointStart, LinePointEnd);
            }
        }

        /// <summary>
        /// 当前是否有鼠标按下，且有矩形被选中
        /// </summary>
        bool _isMouseDown = false;
        /// <summary>
        /// 最后一次鼠标的位置
        /// </summary>
        Point _lastMouseLocation = Point.Empty;
        /// <summary>
        /// 当前被鼠标选中的矩形
        /// </summary>
        RectShape _selectedShape = null;
        /// <summary>
        /// 连线起点
        /// </summary>
        Point LinePointStart = Point.Empty;
        /// <summary>
        /// 连线终点
        /// </summary>
        Point LinePointEnd = Point.Empty;

        /// <summary>
        /// 获取不同的背景颜色
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        Brush GetBrush(int i)
        {
            switch (i)
            {
                case 0: return Brushes.Red;
                case 1: return Brushes.Green;
                case 2: return Brushes.Blue;
                case 3: return Brushes.Orange;
                case 4: return Brushes.Purple;
                default: return Brushes.Red;
            }
        }

        /// <summary>
        /// 计算并生成连线的两个连接点
        /// </summary>
        void CreateLinePoint()
        {

            //计算连线开始点坐标
            var line1X = Shapes[0].Rect.X + Shapes[0].Rect.Width / 2;
            var line1Y = Shapes[0].Rect.Y + Shapes[0].Rect.Height / 2;
            LinePointStart = new Point(line1X, line1Y);

            //计算连线结束点坐标
            var line2X = Shapes[1].Rect.X + Shapes[1].Rect.Width / 2;
            var line2Y = Shapes[1].Rect.Y + Shapes[1].Rect.Height / 2;
            LinePointEnd = new Point(line2X, line2Y);
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            //此处仅显示两个形状
            if (Shapes.Count != 0)
            {
                MessageBox.Show("形状已添加，不可重复操作！");
                return;
            }
            
            //添加2个矩形
            var rs = new RectShape()
            {
                Id = "矩形" + (Shapes.Count + 1),
                Rect = new Rectangle()
                {
                    X = 50,
                    Y = 50,
                    Width = 100,
                    Height = 100,
                },
            };
            Shapes.Add(rs);
            rs = new RectShape()
            {
                Id = "矩形" + (Shapes.Count + 1),
                Rect = new Rectangle()
                {
                    X = 50,
                    Y = 50,
                    Width = 100,
                    Height = 100,
                },
            };
            Shapes.Add(rs);

            //生成连线的两个连接点
            CreateLinePoint();

            //重绘所有矩形
            DrawAll(panel1.CreateGraphics());
        }

        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            //当鼠标按下时

            //取最上方的矩形，也就是最后添加的矩形
            var sp = Shapes.FindLast(a => a.Rect.Contains(e.Location));
            if (sp != null)
            {
                //证明取到了矩形

                //设置状态及选中矩形
                _isMouseDown = true;
                _lastMouseLocation = e.Location;
                _selectedShape = sp;
            }
        }

        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            //当鼠标移动时

            if (_isMouseDown)
            {
                //当且仅当：有鼠标按下且有矩形被选中时，才进行后续操作

                //改变选中矩形的位置信息，随着鼠标移动而移动

                //计算鼠标位置变化信息
                var moveX = e.Location.X - _lastMouseLocation.X;
                var moveY = e.Location.Y - _lastMouseLocation.Y;

                //将选中形状的位置进行同样的变化
                var oldXY = _selectedShape.Rect.Location;
                oldXY.Offset(moveX, moveY);
                _selectedShape.Rect = new Rectangle(oldXY, _selectedShape.Rect.Size);

                //记录当前鼠标位置
                _lastMouseLocation.Offset(moveX, moveY);

                //因为形状位置发生了变化，所以要重新计算连线的两个连接点
                CreateLinePoint();

                //重绘所有矩形
                DrawAll(panel1.CreateGraphics());
            }

        }

        private void panel1_MouseUp(object sender, MouseEventArgs e)
        {
            //当鼠标松开时
            if (_isMouseDown)
            {
                //当且仅当：有鼠标按下且有矩形被选中时，才进行后续操作

                //重置相关记录信息
                _isMouseDown = false;
                _lastMouseLocation = Point.Empty;
                _selectedShape = null;
            }
        }

    }


}
